package com.wnxy.eleba.controller;

import com.github.pagehelper.PageInfo;
import com.wnxy.eleba.entity.Customer;
import com.wnxy.eleba.entity.Customer;
import com.wnxy.eleba.service.CustomerService_zhuying;
import com.wnxy.eleba.service.CustomerService_zhuying;
import com.wnxy.eleba.utils.Result;
import com.wnxy.eleba.vo.CustomerQueryParam;
import com.wnxy.eleba.vo.CustomerQueryParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户控制器
 */

@CrossOrigin
@RestController
@RequestMapping("/customer")
public class CustomerController_zhuying {

    private CustomerService_zhuying customerService;

    public final Logger logger = LoggerFactory.getLogger(CustomerController_zhuying.class);

    @Autowired
    public void setCustomerService(CustomerService_zhuying customerService) {
        this.customerService = customerService;
    }

    @PostMapping("/findAllCustomer")
    public Result findAllCustomer(){
        List<Customer> list = customerService.findAll();
        System.out.println(list);
        return Result.ok().data("list",list);


    }



    @PostMapping("/listCustomerPageByCondition")
    public Result listCustomerPageByCondition(@RequestBody CustomerQueryParam customerQueryParam){
        logger.warn(customerQueryParam.toString());
        PageInfo<Customer> pageInfo = customerService.listCustomerPageByCondition(customerQueryParam);
//        System.out.println(pageInfo);
        return Result.ok().data("pageInfo",pageInfo);
        //return Result.ok().data("total",pageInfo.getTotal()).data("list",pageInfo.getList()).data("pageSize",pageInfo.getPageSize());
    }

    @GetMapping("/findCustomerById")
    public Result findCustomerById(@RequestParam("customerId") Integer customerId){
//        System.out.println("Customerid "+CustomerId);
        logger.warn("customerId:" + customerId);
        Customer customer = customerService.findCustomerById(customerId);
        return Result.ok().data("Customer",customer);
    }

    @PostMapping("/save")
    public Result saveCustomer(@RequestBody Customer customer){
        logger.warn("save Customer:" + customer);
        boolean result = customerService.saveCustomer(customer);
        logger.warn("save result:" + result);
        System.out.println(customer);
        return Result.ok().success(result);
    }

    @PostMapping("/update")
    public Result updateCustomer(@RequestBody Customer customer){
        boolean result = customerService.updateCustomer(customer);
        return Result.ok().success(result);
    }

    @PostMapping("/delete")
    public Result deleteCustomer(@RequestParam("ids")Integer... ids){
//        boolean result = customerService.deleteCustomer(ids);
//        System.out.println("2222");
//        return Result.ok().success(result);
        return Result.ok();
    }

    @GetMapping("/del")
    public Result delCustomer(@RequestParam("id")Integer id){


        boolean result = customerService.delCustomer(id);
        System.out.println("2222");
        return Result.ok().success(result);
    }



}
